#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "/usr/local/include/mysql/mysql.h"
#include <syslog.h>
#include "user.h"
#include "item.h"
#include "summon.h"

static char db_host[32] = "192.168.10.137"; 
static char db_user[32] = "aslan"; 
static char db_passwd[32] = ""; 
static char db_name[32] = "aslan_db"; 
MYSQL mysql; 

int db_init()
{
	if( mysql_init(&mysql) == NULL ) 
	{ 
		fprintf(stderr,"Init mysql err!"); 
		return 0; 
	} 

	if (!mysql_real_connect(&mysql, db_host, db_user, db_passwd, db_name, 0, NULL, 0)) 
	{ 
		fprintf(stderr,"Connect to mysql Error:%s!",mysql_error(&mysql)); 
		return 0; 
	} 

}

int db_close()
{
	mysql_close(&mysql); 
}

MYSQL_RES * query(MYSQL *mysql, char *sql) 
{ 
   MYSQL_RES *m_res; 
   
   if(mysql_query(mysql, sql) != 0) 
   { 
	  fprintf(stderr, "mysql_query err: %s", mysql_error(mysql)); 
   } 
   m_res = mysql_store_result(mysql); 
   if(m_res==NULL) 
   { 
	  fprintf(stderr, "get result err: %s", mysql_error(mysql)); 
   } 

}

struct user* db_load_user(int id)
{
   	MYSQL_RES *m_res; 
   	MYSQL_ROW m_row; 
	struct user *user = NULL;
	struct list_head summons;
	struct list_head items;
	struct list_head *pos;
	struct strlist *slist;
	struct item *item;
	struct summon *summon;
	char *sql = alloc_string(100);

	INIT_LIST_HEAD(&summons);
	INIT_LIST_HEAD(&items);
	sprintf(sql, "select * from user where iId=%d", id);
	m_res = query(&mysql, sql); 
	if((m_row = mysql_fetch_row(m_res)))
	{ 
		user = new_user(atoi(m_row[USER_I_ID]), 
				m_row[USER_C_NAME],
				atoi(m_row[USER_I_SEX]),
				atoi(m_row[USER_I_SCHOOL]),
				atoi(m_row[USER_I_HP]),
				atoi(m_row[USER_I_HPEFF]),
				atoi(m_row[USER_I_MP]),
				atoi(m_row[USER_I_STRONG]),
				atoi(m_row[USER_I_WISDOM]),
				atoi(m_row[USER_I_PHYSIQUE]),
				atoi(m_row[USER_I_AGILE]),
				atoi(m_row[USER_I_WEN]),
				m_row[USER_C_SKILL],
				m_row[USER_C_FACE],
				m_row[USER_C_EQUIPA],
				m_row[USER_C_EQUIPB],
				m_row[USER_C_EQUIPC],
				m_row[USER_C_EQUIPD],
				m_row[USER_C_EQUIPE],
				m_row[USER_C_EQUIPF],
				m_row[USER_C_EQUIPG]
				);
		
		string_split(m_row[USER_C_SUMMON], ":", &summons);
		list_for_each_entry(slist, &summons, list)
		{
			summon = db_load_summon(atoi(slist->str));
			if (summon != NULL)
			{
				add_user_summon(user, summon);
			}
		}

		string_split(m_row[USER_C_ITEM], ":", &items);
		list_for_each_entry(slist, &items, list)
		{
			item = db_load_item(atoi(slist->str));
			if (item != NULL)
			{
				add_user_item(user, item);
			}
		}

	} 
	
	free_strlist(&summons);
	free_strlist(&items);
	free_string(sql);	
   	mysql_free_result(m_res); 
	
	return user;
}

struct item* db_load_item(int id)
{
	MYSQL_RES *m_res; 
   	MYSQL_ROW m_row; 
	struct item *item = NULL;
	char *sql = alloc_string(100);
	
	sprintf(sql, "select * from item where iId=%d", id);
	m_res = query(&mysql, sql); 
	if((m_row = mysql_fetch_row(m_res)))
	{ 
		item = new_item(atoi(m_row[ITEM_I_ID]), 
				m_row[ITEM_C_NAME],
				atoi(m_row[ITEM_I_OWNER]),
				atoi(m_row[ITEM_I_POS]),
				atoi(m_row[ITEM_I_FRAME]),
				atoi(m_row[ITEM_I_AMOUNT]),
				atoi(m_row[ITEM_I_HARDINESSMAX]),
				atoi(m_row[ITEM_I_HARDINESS]),
				atoi(m_row[ITEM_I_TYPE]),
				m_row[ITEM_C_SHAPE]
				);	
	} 
	
	free_string(sql);	
   	mysql_free_result(m_res); 
	
	return item;

}

struct summon* db_load_summon(int id)
{
	MYSQL_RES *m_res; 
   	MYSQL_ROW m_row; 
	struct summon* summon = NULL;
	char *sql = alloc_string(100);
	
	sprintf(sql, "select * from summon where iId=%d", id);
	m_res = query(&mysql, sql); 

	if((m_row = mysql_fetch_row(m_res)))
	{ 
		summon = new_summon(atoi(m_row[SUMMON_I_ID]), 
					m_row[SUMMON_C_NAME],
					atoi(m_row[SUMMON_I_TYPE]),
					atoi(m_row[SUMMON_I_OWNER]),
					atoi(m_row[SUMMON_I_HP]),
					atoi(m_row[SUMMON_I_MP]),
					atoi(m_row[SUMMON_I_EXP]),
					m_row[SUMMON_C_SKILL],
					atoi(m_row[SUMMON_I_POS]),
					atoi(m_row[SUMMON_I_FRAME]),
					atoi(m_row[SUMMON_I_POINT]),
					atoi(m_row[SUMMON_I_STRONG]),
					atoi(m_row[SUMMON_I_TALENT]),
					atoi(m_row[SUMMON_I_AGILE]),
					atoi(m_row[SUMMON_I_WISDOM]),
					atoi(m_row[SUMMON_I_RNSAVVY]),
					atoi(m_row[SUMMON_I_WLSAVVY]),
					atoi(m_row[SUMMON_I_JSSAVVY]),
					atoi(m_row[SUMMON_I_TPSAVVY]),
					atoi(m_row[SUMMON_I_MJSAVVY])
					);	
	
	} 
	
   	mysql_free_result(m_res); 
	
	return summon;

}
